package com.orangelife.mobile.voucher.biz;

import android.os.Handler;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.confirmOrder.biz.MerchantOrderListHelper;
import com.orangelife.mobile.confirmOrder.entity.MerchantInfo;
import com.orangelife.mobile.constants.VoucherURL;
import com.orangelife.mobile.util.JsonReponseHandler;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抵用券服务端接口
 * Created by wgf on 2016/2/26.Modify by wgf on 20160324
 *
 * @author wgf
 * @since v3.4.2
 */
public class VoucherHttpBiz {
    /**
     * 商品，包括周边商户和单个商品
     */
    public static final int TYPE_GOOD = 1;
    /**
     * 物业维修抵用券
     */
    public static final int TYPE_REPAIR = 2;
    /**
     * 洗车抵用券
     */
    public static final int TYPE_WASH_CAR = 3;

    /** 物业缴费 */
    public static final int TYPE_PROPERTY = 5;

    /**获取我的所有的抵用券*/
    public static void getMyList(int pageNo, int pageSize, Handler handler) {
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("pageIndex", String.valueOf(pageNo));
        urlMap.put("pageSize", String.valueOf(pageSize));
        VolleyHttpUtil.getInstance().getWithHandler(VoucherURL.MY_LIST, handler, 0, urlMap);
    }

    /**
     * 获取抵用券列表，商品列表模式
     */
    public static void getVoucherListWithMerchant(int type, String merID, String totalCost, VolleyHttpUtil.ResponseCallBack callBack) {
        getVoucherList(type, merID, totalCost, null, callBack);
    }

    /**
     * 请求抵用券列表
     * wiki地址：
     *
     * @param type      商品、维修、停车卡
     * @param merID
     * @param totalCost 满减用
     * @param goodID
     */
    public static void getVoucherList(int type, String merID, String totalCost, String goodID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(MerchantOrderListHelper.TYPE, type);
        if (goodID != null) {/*DD此车，单商品*/
            map.put(MerchantOrderListHelper.GOOD_ID, goodID);
        }
        if (totalCost != null) {
            map.put(MerchantOrderListHelper.TOTAL_COST, totalCost);
        }
        /*type=1必填，周边商户商品时使用此参数*/
        if (merID != null) {
            map.put(MerchantOrderListHelper.MER_ID, merID);
        }
//        ArrayList<String> otherVoucherList = SelectedVoucherIDListHelper.getInstance().getOtherVoucherList();
        /*其他商户已选择的抵用券ID列表*/
//        map.put(.OTHER_VOUCHER_LIST, otherVoucherList);
        /*当前商户已选择的抵用券ID列表，目前服务端并未使用此参数*/
//        map.put(.SELF_VOUCHER_LIST, selfVoucherList);
//        JSONRequest.getInstance().sendRequest(JSONRequest.POST, URL_AVAILABLE_VOUCHER_LIST, map, handler, what, HttpUtil.ACTION_JSON);
        VolleyHttpUtil.getInstance().postWithCallback(VoucherURL.AVAILABLE_LIST, map, callback);
    }

    public static List<Map<String, Object>> handleVoucherList(JSONObject response, FatherAdapter adapter) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            List<Map<String, Object>> voucherList = JsonReponseHandler.getListFromJson(
                    response.toString(), adapter, null);
            return voucherList;
        }
        return null;
    }

    /** 商品(DD洗车,物业缴费[merID=commID,goodID=apartmentID]); */
    public static void getVoucherHighestPriceListWithGood(String merID, String goodID, String money, int type, VolleyHttpUtil.ResponseCallBack callback) {
        List<Map<String, Object>> merList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("merID", merID);
        map.put("money", money);
        map.put("goodID", goodID);/*DD洗车才有用*/
        merList.add(map);
        getVoucherHighestPriceList(merList, type, callback);
    }

    /** 服务 */
    public static void getVoucherHighestPriceListWithService(String totalCost, int type, VolleyHttpUtil.ResponseCallBack callback) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("money", totalCost);
        list.add(map);
        getVoucherHighestPriceList(list, type, callback);
    }

    /** 商户 */
    public static void getVoucherHighestPriceListWithMerchant(List<MerchantInfo> merchantInfoList, int type, VolleyHttpUtil.ResponseCallBack callback) {
        List<Map<String, Object>> merList = new ArrayList<>();
        for (int i = 0; i < merchantInfoList.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            MerchantInfo mi = merchantInfoList.get(i);
            String merID = mi.getMerID();/*商户ID*/
            String goodID = mi.getGoodsID();/*商品ID*/
            if (null != merID) {
                map.put("merID", merID);
            }
            map.put("goodID", goodID);
            map.put("money", mi.getSingleTotalPrice());
            merList.add(map);
        }
        getVoucherHighestPriceList(merList, type, callback);
    }

    /*总*/
    private static void getVoucherHighestPriceList(List<Map<String, Object>> buyInfo, int type, VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().cancel("TAG_VOUCHER_HIGHEST");
        HashMap<String, Object> mapVoucher = new HashMap<>();
        mapVoucher.put("type", type);/*周边商品（包括DD洗车）、维修、停车卡*/
        mapVoucher.put("buyInfo", buyInfo);/*merID": "商户","money": "商品总额","goodID "", */
        VolleyHttpUtil.getInstance().postWithCallback(VoucherURL.HEIGHT_PRICE_LIST, mapVoucher, callback);
    }

    public static void cancelVoucherHighestPriceList() {
        VolleyHttpUtil.getInstance().cancel("TAG_VOUCHER_HIGHEST");
    }

    public static List<Map<String, Object>> handleVoucherHighestPriceList(JSONObject response) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);
        }
        return list;
    }


    /**
     * 删除消息
     */
    public static void deleteVoucher(String voucherID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("sysCouponID", voucherID);
        VolleyHttpUtil.getInstance().delWithCallback(VoucherURL.DELETE, map, callback);
    }

}
